import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    history:true,//显示历史搜索
    historyList:[],//历史搜索标签
    placeholder:'',//

    active:0,
    list:[],
    searchval:'',//搜索关键字
    module:'activity',//
    action:'new',//
    page:1,
    pagesize:10,
    hd_no_flag:0,
    qn_no_flag:0,
    zx_no_flag:0,
  },
  //点击搜索历史 label
  showList(e){
    let name = e.currentTarget.dataset.name
    let id = e.currentTarget.dataset.id
    let type = 1//e.currentTarget.dataset.type
    // 1 是活动 2 是青年之家 3是团的机关
    let active 
    switch (type){
          case 1:
              active = 0; 
              break;
          case 2:
              active = 1;
              break;
          case 3:
              active = 2;
              break;
         default:
        //        active = 0; 
      }

      this.setData({
        active,
        history:false,
        searchval:name,
        page:1,list:[]
      })

      this.getActive(this.data.module,this.data.action,this.data.searchval) 
  },
  //历史搜索记录
  async getHistoryList(){
    let data={}
    data['flag'] = '_search_history'
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      this.setData({
        historyList:result,
      })
    }else {
      // Toast(message)
    }
  },
  //清空搜索
  async delhistory(){
    let data={}
    data['flag'] = '_search_history_clear'
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      this.setData({
        historyList:[],
      })
    }else {
      // Toast(message)
    }
  },

  //搜索
  onSearch(e){
    let searchval;
    if(e.detail == ""  && this.data.placeholder != ""){
      searchval = this.data.placeholder
    }else{
      searchval = e.detail
    }
    this.setData({
      searchval:searchval,page:1,list:[],history:false,pagesize:10,placeholder:'',
    })
    this.getActive(this.data.module,this.data.action,this.data.searchval) 
  },
  onCancel(e){
    this.setData({
      searchval:e.detail,page:1,list:[],history:false,pagesize:10,placeholder:'',
    })
    this.getActive(this.data.module,this.data.action,this.data.searchval)
  },
  //tab切换
  onChange(e){
    let ind = e.detail.index;
    let module,action
    if(ind == 0){
      module = 'activity'
      action = 'new'
    }else if(ind == 1){
      module = 'youthclub'
      action = 'recently'
    }else if(ind == 2){
      module = 'information'
      action = ''
    }
    this.setData({
      module:module,
      action:action,page:1,list:[]
    })
    this.getActive(this.data.module,this.data.action,this.data.searchval)
  },
   // 当自定义组件触发 parentEvent 事件时，调用 onParentEvent 方法
   onParentEvent: function (event) {
    // 自定义组件触发事件时提供的detail对象，用来获取子组件传递来的数据
    let module = event.detail.module;
    let action = event.detail.action;
    this.setData({
      module:module,
      action:action,page:1,list:[]
    })
    this.getActive(this.data.module,this.data.action,this.data.searchval)
  },

  //搜索-获取活动数据
  async getActive (module,action,keywords) {
    let data={}
    data['flag'] = '_search_'
    data['module'] = module
    data['action'] = action
    data['keywords'] =keywords
    data['location[lng]'] = app.globalData.currpoint.longitude
    data['location[lat]'] = app.globalData.currpoint.latitude
    data['page'] = this.data.page
    data['pagesize'] = this.data.pagesize
    
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result && result.forEach((item)=>{
        if ( item.wj_url ){
          let imgArr = item.wj_url.split(',')
          item.wj_url = api.checkurl(imgArr[0])
          item.imgurl = item.wj_url
        }
        if( item.imgurl && item.imgurl.length>0 ){
          item.imgurl = api.checkurl(item.imgurl)
        }
        item.activity && item.activity.forEach((item1)=>{
          if( item1.picurl ){
            item1.picurl = item1.picurl.split(',')
            item1.picurl = api.checkurl(item1.picurl)
            item1.picurl = item1.picurl[0]
          }
        })
      })

      if( result && result.length>0 ){
        let page = this.data.page+1
        let str = 'list['+(this.data.page-1)+']'
        this.setData({
          [str]:result,page,
          hd_no_flag:0,
          qn_no_flag:0,
          zx_no_flag:0
        })
      }else{
        if( module=='activity') this.setData({hd_no_flag:1})
        if( module=='youthclub') this.setData({qn_no_flag:1})
        if( module=='information') this.setData({zx_no_flag:1})
      }
    
    }else {
      // Toast(message)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getHistoryList()
    this.setData({
      placeholder:options.name
    })
    // this.getActive(this.data.module,this.data.action,this.data.searchval)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getActive(this.data.module,this.data.action,this.data.searchval)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})